<?php
include 'system/common.inc.php';
if(!$_GET['sid']) throw new Exception('Illegal API request.');
if(in_array($_GET['sid'], $blacklist)) throw new Exception('Illegal API request.');
$_POST['parm'] = $_GET['parm'];
list($username, $password, $formhash) = decypt_request();
if($client['key'] == 'BLOCKED') showmessage('该网站在黑名单中，禁止使用此功能<br></p><ins>可能的原因：该网站非法盗取用户数据或篡改程序版权或将本软件用于商业用途</ins><br><p style="display: none">', 'http://www.kookxiang.com', 7);
if(inBlackList($client['url'])) showmessage('该网站在黑名单中，禁止使用此功能<br></p><ins>可能的原因：该网站非法盗取用户数据或篡改程序版权或将本软件用于商业用途</ins><br><p style="display: none">', 'http://www.kookxiang.com', 7);
if(!$username || !$password || !$formhash) throw new Exception('Illegal package');
if(!$client['url']) header('Location: http://www.kookxiang.com/thread-2022-1-1.html');
if($_POST['cookie']) prase_post_cookie();
if(!$_POST['token']){
	// 得到UID
	fetch('https://passport.baidu.com/v2/api/?getapi&tpl=tb&apiver=v3&tt='.time().'520&class=login&logintype=dialogLogin&callback=bd__cbs__sbw');
	// 得到token
	$token_query = fetch('https://passport.baidu.com/v2/api/?getapi&tpl=tb&apiver=v3&tt='.time().'520&class=login&logintype=dialogLogin&callback=bd__cbs__sbw');
	preg_match('/"token" : "(\w+)"/', $token_query, $match);
	$token = $match[1];
}else{
	$token = $_POST['token'];
}

if($_POST['author'] && strtolower($_POST['author'])!= 'kookxiang'){
	echo '<script type="text/javascript">alert("作者信息有误，请重新填写");</script>';
	exit();
}

$data = array();
$data['apiver'] = 'v3';
$data['callback'] = 'parent.bd__pcbs__sbw';
$data['charset'] = 'UTF-8';
$data['codestring'] = $_POST['codestring'];
$data['isPhone'] = 'false';
$data['logintype'] = 'bascilogin';
$data['mem_pass'] = 'on';
$data['password'] = urlencode($password);
$data['ppui_logintime'] = '8888';
$data['quick_user'] = '0';
$data['safeflg'] = '0';
$data['splogin'] = 'rate';
$data['staticpage'] = 'http://tieba.baidu.com/tb/static-common/html/pass/v3Jump.html';
$data['token'] = $token;
$data['tpl'] = 'tb';
$data['tt'] = time().'520';
$data['u'] = 'http://tieba.baidu.com/';
$data['username'] = urlencode($username);
$data['verifycode'] = $_POST['verifycode'];
// 尝试登录
$data_str = '';
foreach($data as $key => $value){
	$data_str .= "{$key}={$value}&";
}
$result = fetch('https://passport.baidu.com/v2/api/?login', $data_str);

if(!$result){
	echo '<script type="text/javascript">alert("登陆超时，过会再试吧");</script>';
}elseif(strexists($result, 'captchaservice')){
	if(strexists($result, 'err_no=400031')){
		echo '<script type="text/javascript">alert("你已经开启了登陆保护，请先在 passport.baidu.com 关闭登陆保护");</script>';
	} elseif(strexists($result, 'err_no=4')) {
		echo '<script type="text/javascript">alert("无法登陆百度通行证，可能是用户名或密码有误！");</script>';
	} elseif (preg_match('/(captchaservice\w{200,})/', $result, $match)){
		$verifyAddress = $match[1];
		if($_POST['codestring']){
			//echo '<!-- '.$result.' -->';
			echo '<script type="text/javascript">alert("貌似登陆失败了……\n\n(这就是传说中的RPWT么？)\n要不试试下面的手动绑定吧");</script>';
		}
	}
}else{
	$cookie = get_cookie_string();
	$return = bin2hex(authcode($cookie, 'ENCODE', $client['key']));
	$url = "{$client[url]}api.php?action=receive_cookie&formhash={$formhash}";
	if(strexists($url, '.rhcloud.com')) $url = str_replace('http://', 'https://', $url);
	//header("Location: {$url}");
	echo '登陆成功，请稍候...<form method="post" action="'.$url.'" style="display: none" id="redirect"><input type="hidden" name="cookie" value="'.$return.'"><input type="submit"></form><script type="text/javascript">document.getElementById("redirect").submit();</script>';
	exit();
}

function fetch($url, $postdata = '', $cookie = ''){
	if(!$cookie) $cookie = get_cookie_string();
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_TIMEOUT, 6);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	if($postdata) curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
	$get_url = curl_exec($ch);
	if($get_url !== false){
		$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		if ($statusCode >= 500) return false;
  	}
	prase_cookie($get_url);
    curl_close($ch);
    return $get_url;
}
function get_cookie_string(){
	global $CURL_COOKIE;
	$str = '';
	foreach ($CURL_COOKIE as $key => $val){
		if($key) $str .= "{$key}={$val}; ";
	}
	return $str;
}
function prase_cookie($header){
	global $CURL_COOKIE;
	if (preg_match_all('/Set-Cookie: ([^=]+)=([^;]+)/', $header, $match)){
		foreach ($match[1] as $key => $val){
			$k = trim($match[1][$key]);
			if(!$k) continue;
			if($k == 'BAEID') continue;
			$CURL_COOKIE[$k] = trim($match[2][$key]);
		}
	}
}
function prase_post_cookie(){
	global $CURL_COOKIE;
	if (preg_match_all('/([^=;]+)=([^;]+)/', $_POST['cookie'], $match)){
		foreach ($match[1] as $key => $val){
			$k = trim($match[1][$key]);
			if(!$k) continue;
			$CURL_COOKIE[$k] = trim($match[2][$key]);
		}
	}
}

list($domain) = explode('/', $client['host']);
?>
<!DOCTYPE html>
<html>
<head>
<title>账号绑定 - 贴吧签到助手</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="HandheldFriendly" content="true" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<meta name="author" content="kookxiang" />
<meta name="copyright" content="KK's Laboratory" />
<link rel="shortcut icon" href="/favicon.ico" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<link rel="stylesheet" href="style/main.css" type="text/css" />
<link rel="stylesheet" href="api_login.css" type="text/css" />
</head>
<body>
<div class="wrapper" id="page_login">
<div class="center-box">
<h1>绑定账号</h1>
<form method="post" action="login.php?sid=<?php echo $_GET['sid']; ?>&parm=<?php echo $_GET['parm']; ?>" onsubmit="document.getElementById('submit').disabled=true">
<div class="login-info">
<p>合作网站： 贴吧签到助手 (<?php echo $domain; ?>)</p>
<p>百度通行证： <?php echo $username; ?></p>
<p>通行证密码： ******</p>
<?php
if($verifyAddress){
	$cookie = $cookie ? $cookie : get_cookie_string();
	echo <<<EOF
<p>验证码：
<img src="https://passport.baidu.com/cgi-bin/genimage?{$verifyAddress}" class="verifycode" onclick="this.src=this.src+'&'" />
<input type="text" name="verifycode" placeholder="请输入验证码" autocomplete="off" required />
<input type="hidden" name="token" value="{$token}" />
<input type="hidden" name="cookie" value="{$cookie}" />
<input type="hidden" name="codestring" value="{$verifyAddress}" /></p>
EOF;
}
?>
<p><a href="manual_bind.php?sid=<?php echo $_GET['sid']; ?>&formhash=<?php echo $formhash; ?>">自动登录有问题？尝试手动绑定</a></p>
</div>
<p>贴吧签到助手的作者是： <input type="text" name="author" placeholder="koo...." value="<?php echo $_POST['author']; ?>" required style="width: 120px" /> <a href="http://www.kookxiang.com/thread-2699-1-1.html" target="_blank">不知道？</a></p>
<p><label><input type="checkbox" name="readme_2" value="1" <?php if($_POST['readme_2']) echo 'checked'; ?> required /> 我知道 <?php echo $domain; ?> 可以获得我的百度账号信息</label></p>
<p class="btns">
<input type="submit" id="submit" value="绑定账号" />
<button onclick="window.close();">返回网站</button>
</p>
</form>
</div>
<p class="copyright">Designed by <a href="http://www.ikk.me" target="_blank">kookxiang</a> - <a href="http://go.ikk.me/donate" target="_blank">赞助开发</a><br>All right reserved, 2013 &copy; <a href="http://www.kookxiang.com" target="_blank">KK's Laboratory</a></p>
</div>
</body>
</html>